import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';

/**
 *
 * @Description TODO
 * @Author liuer
 * @CreateTime 2024年01月13日 13:42
 */
class DeviceEsGroupView extends StatelessWidget {
  final List list;

  const DeviceEsGroupView(this.list, {super.key});

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      padding: EdgeInsets.zero,
      itemBuilder: (BuildContext context, int index) {
        return _itemWidget(index);
      },
      itemCount: list.length,
    );
  }

  _itemWidget(
    int index,
  ) {
    Map map = list[index];
    String value = map['value'].toString();
    if (map['dataType'] != null &&
        map['dataType']['specs'] != null &&
        map['dataType']['specs']['unit'] != null &&
        map['dataType']['specs']['unit'].toString().isNotEmpty) {
      value = '$value(${map['dataType']['specs']['unit']})';
    }
    var masterSlaveMode = '--';
    if (map['masterSlaveMode'] != null) {
      if (map['masterSlaveMode'].toString() == '0') {
        masterSlaveMode = ResKey.host.tr;
      } else if (map['masterSlaveMode'].toString() == '1') {
        masterSlaveMode = ResKey.slave.tr;
      }
    }
    Log.d('map===' + map.toString());
    return Container(
      // color: Colors.red,
      // margin: EdgeInsets.only(left: 16.w, right: 16.w),
      // padding: const EdgeInsets.only(top: 15),
      child: Column(
        children: [
          Container(
              height: 53,
              padding: EdgeInsets.only(left: 16.w),
              color: Colors.white,
              alignment: Alignment.centerLeft,
              child: Text(
                map['name'] ?? '--',
                style: ytTextStyle(fontSize: 18, fontWeight: FontWeight.w500),
              )),
          Container(
            // height: 120,
            color: Colors.white,
            // padding: EdgeInsets.only(left: 16.w, right: 16.w),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [

              _itemRowWidget('${'device.parallelMachineNum'.tr}：', '${index + 1}#'),
                _itemRowWidget('${ResKey.masterSlaveMark.tr}：', masterSlaveMode),

                _itemRowWidget(
                  '${'siteMonitor.communicationStatus'.tr}：',
                  (map['connectStatus'] == null ||
                          map['connectStatus'].toString().isEmpty)
                      ? '--'
                      : map['connectStatus'].toString() == '1'
                          ? ResKey.onLine.tr
                          : ResKey.offLine.tr),
                  _itemRowWidget('${ResKey.ipAddress.tr}：',
                  map['ipAddress'] ?? '--',
                ),
                _itemRowWidget(
                  '${'device.systemWorkMode'.tr}: ',
                  map['${map['systemWorkingStatus']}-${map['deviceId']}'] ??
                      '--'),
                _itemRowWidget('${'device.systemWorkStatus'.tr}：',
                  map['${map['systemWorkModelId']}-${map['deviceId']}'] ?? '--',
                ),
                _itemRowWidget('SOC：', map['soc'] == null? '--':map['soc'].toString(),isShowLine: false),
                Gaps.vGap5
              ],
            ),
          ),
          Visibility(
              visible: index != list.length - 1,
              child: Container(
                height: 10,
                width: ScreenUtil().screenWidth,
                color: Colour.backgroundColor,
              ))
        ],
      ),
    );
  }

  _itemRowWidget(
      String title, String realValue,{bool isShowLine = true}) {
    return Container(
      alignment: Alignment.centerLeft,
      height: 50,
      color: Colors.white,
      child: Column(
        children: [
          SizedBox(
            height: 49,
            child: Row(
              children: [
                Gaps.hGap16,
                Expanded(
                  child: Container(
                    child: Text(
                      title,
                      maxLines: 2,
                      style: ytTextStyle(
                          fontSize: 14, color: Colour.otherTextColor),
                    ),
                  ),
                ),
                Container(
                  width: 150.w,
                  alignment: Alignment.centerRight,
                  child: Text(
                    realValue,
                    textAlign: TextAlign.end,
                    maxLines: 2,
                    style: ytTextStyle(fontSize: 14, color: Colour.mainTextColor),
                  ),
                ),
                Gaps.hGap16,
              ],
            ),
          ),
          if(isShowLine)Container(
            margin: EdgeInsets.only(left: 16.w, right: 16.w),
            height: 1,
            color: Colour.backgroundColor,
          )
        ],
      ),
    );
  }
  _itemRow(String title1, dynamic value1, String title2, dynamic value2) {
    return Row(
      children: [
        SizedBox(
          width: ScreenUtil().screenWidth * 0.5 - 16.w,
          child: Row(
            children: [
              SizedBox(
                  width: 120,
                  // color: Colors.red,
                  child: Text(
                    title1,
                    maxLines: 2,
                    style:
                        ytTextStyle(fontSize: 14, color: Colour.labelTextColor),
                  )),
              Text(
                value1.toString(),
                maxLines: 1,
                style: ytTextStyle(
                  fontSize: 14,
                ),
              )
            ],
          ),
        ),
        // const Spacer(),
        SizedBox(
          width: ScreenUtil().screenWidth * 0.5 - 16.w,
          child: Row(
            children: [
              Container(
                  width: 120,
                  // color: Colors.green,
                  child: Text(
                    title2,
                    maxLines: 2,
                    style:
                        ytTextStyle(fontSize: 14, color: Colour.labelTextColor),
                  )),
              Text(
                value2.toString(),
                maxLines: 1,
                style: ytTextStyle(
                  fontSize: 14,
                ),
              )
            ],
          ),
        ),
      ],
    );
  }
}
